#!/bin/bash
a=`date +%Y%m%d`;#获取当前时间
start(){
    echo "start project..."
	if [[ ! -d ./pid/ ]];
	then    
       mkdir -p pid;
	fi;
	if [[ ! -d ./serverLog/ ]];
	then    
       mkdir -p serverLog;
	fi;
	java -jar cock-0.0.1-SNAPSHOT.jar  cock > ./serverLog/{$a}_cock.log   &       #注意：必须有&让其后台执行，否则没有pid生成
	echo $! > ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid   # 将jar包启动对应的pid写入文件中，为停止时提供pid
	echo " " > ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid # 多个pid分隔
    cat ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid
    echo "start project end..."
}

stop(){
    echo "stop project..."
	PID=`cat ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid`
	kill -9 $PID
	echo "stop project end..."
	rm -rf ./pid
}
#停止指定的服务
stopAppoint(){
	echo 'Stop the specified service'
	echo 'stop  cock------>1'
	read aNum
	case $aNum in
		1)  echo 'your choose cock'
		PID=$(cat ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid)
		kill -9 $PID
		rm -rf ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid
		exit;
		;;
		*) echo 'There are no numbers you enter'
		exit 1
		;;
	esac

}

#启动指定的服务
startAppoint(){
	echo 'Start the specified service'
	echo 'Start  cock------>1'
	if [[ ! -d ./pid/ ]];
	then    
       mkdir -p pid;
	fi;
	read aNum
	case $aNum in
		1)  echo 'your choose cock'
		java -jar dhcc-eureka-0.0.1-SNAPSHOT.jar  cock > ./serverLog/{$a}_cock.log   &       #注意：必须有&让其后台执行，否则没有pid生成
		echo $! > ./pid/dhcc-cock-0.0.1-SNAPSHOT.pid   # 将jar包启动对应的pid写入文件中，为停止时提供pid
		;;
		*) echo 'There are no numbers you enter'
		exit 1
		;;
	esac
}

restart(){
    stop   
    sleep 1   
    start
}

if [[ $# -eq 1 ]]; then
    case $1 in
        start | begin)
          start
        ;;
        stop | end)
          stop
        ;;
		restart)
          restart
        ;;
        *)
          echo "Ignorant"
        ;;
	esac
else
    echo "Input error"
fi